package com.nulstudio.emotion.text.controller;

import com.nulstudio.emotion.text.common.NulResult;
import com.nulstudio.emotion.text.entity.NulTextEntry;
import com.nulstudio.emotion.text.request.DeleteHistoryRequest;
import com.nulstudio.emotion.text.service.TextRecognitionService;
import org.springframework.lang.NonNull;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/recognition")
public class TextRecognitionController {

    private final TextRecognitionService textRecognitionService;

    public TextRecognitionController(@NonNull TextRecognitionService textRecognitionService) {
        this.textRecognitionService = textRecognitionService;
    }

    @GetMapping("/getHistory")
    @PreAuthorize("hasAuthority('text:read')")
    public NulResult<List<NulTextEntry>> getHistory(
            @RequestParam(defaultValue = "") @NonNull String filter
    ) {
        return NulResult.response(textRecognitionService.getCurrentUserHistories(filter));
    }

    @PostMapping("/analyzeStrings")
    @PreAuthorize("hasAuthority('text:write')")
    public NulResult<List<Boolean>> analyzeStrings(@RequestBody List<String> strings) {
        return NulResult.response(textRecognitionService.analyzeStrings(strings));
    }

    @PostMapping("/deleteHistory")
    @PreAuthorize("hasAuthority('text:write')")
    public NulResult<Void> deleteHistory(@RequestBody @NonNull DeleteHistoryRequest request) {
        textRecognitionService.deleteHistory(request.id());
        return NulResult.response();
    }
}